﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_DAL;
using System.Data.Common;
using Nova_Common;
using System.Text;
using System.Collections.Generic;

public partial class System_Group_Group : System.Web.UI.Page
{

    DbHelper dbHelper = new DbHelper();
    DataSet ds = new DataSet();
    Nova_MOD.Group Nova_MOD_Group = new Nova_MOD.Group();
    Nova_BLL.Group Nova_BLL_Group = new Nova_BLL.Group();

    protected int iCount = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        LoginLogic.IsLogin();

        if (!IsPostBack)
        {
            gv_Bind();
        }

        //验证TextBox
        string strJs = "";
        strJs = "return Validate(";
        strJs += "'txtvGroupNo','工作组编号不为空','r'";
        strJs += ",'txtvGroupName','工作组名称不为空','r'";
        strJs += ");";
        ibtnSubmit.Attributes.Add("onclick", strJs);
    }

    /// <summary>
    /// 工作组列表
    /// </summary>
    protected void gv_Bind()
    {
        ds = Nova_BLL_Group.DataListGroup();
        gvGroup.DataSource = ds;
        gvGroup.DataBind();
    }

    /// <summary>
    /// 添加工作组
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Nova_MOD_Group.vGroupNo = txtvGroupNo.Text.ToString().Trim();
            Nova_MOD_Group.vGroupName = txtvGroupName.Text.ToString().Trim();
            iCount = Nova_BLL_Group.AddGroup(Nova_MOD_Group);

            if (iCount == 1)
            {
               
                //ShowBox.MessageBox(this.Page, "添加成功!");
                gv_Bind();
                ibtnReset_Click(sender, e);//清空                
                return;
            }
            else if (iCount == 2)
            {
                ShowBox.MessageBox(this.Page, "工作组编号已经存在!");
                return;

            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    /// <summary>
    /// 清空
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
    {
        txtvGroupNo.Text = "";
        txtvGroupName.Text = "";
    }

    /// <summary>
    /// 取消
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvGroup_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvGroup.EditIndex = -1;
        gv_Bind();
    }

    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvGroup_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvGroup.EditIndex = e.NewEditIndex;
        gv_Bind();
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvGroup_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string strID = gvGroup.DataKeys[e.RowIndex].Values[0].ToString();
        string oldGroupNo = gvGroup.DataKeys[e.RowIndex].Values[1].ToString();
        TextBox txt_vGroupNo = (TextBox)gvGroup.Rows[e.RowIndex].FindControl("txt_vGroupNo");
        TextBox txt_vGroupName = (TextBox)gvGroup.Rows[e.RowIndex].FindControl("txt_vGroupName");
       
        if (txt_vGroupNo.Text.Trim() == "")
        {
            ShowBox.MessageBox(this.Page, "工作组编号不为空!");
            return;
        }
        if (txt_vGroupName.Text.Trim() == "")
        {
            ShowBox.MessageBox(this.Page, "工作组名称不为空!");
            return;
        }

        try
        {
            Nova_MOD_Group.PID = strID;
            Nova_MOD_Group.vGroupNo = txt_vGroupNo.Text.ToString().Trim();
            Nova_MOD_Group.vGroupName = txt_vGroupName.Text.ToString().Trim();
            iCount = Nova_BLL_Group.UpdateGroup(Nova_MOD_Group,oldGroupNo);

            if (iCount == 1)
            {
                //ShowBox.MessageBox(this.Page, "修改成功!");
                gvGroup.EditIndex = -1;
                gv_Bind();
                return;
            }
            else if (iCount == 2)
            {
                ShowBox.MessageBox(this.Page, "工作组编号已经存在!");
                return;

            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton ibtnDelete = (ImageButton)sender;
        Nova_MOD_Group.PID = ibtnDelete.CommandArgument.Trim();

        if (!string.IsNullOrEmpty(ibtnDelete.CommandArgument))
        {
            try
            {
                iCount = Nova_BLL_Group.DeleteGroup(Nova_MOD_Group.PID.ToString());
                if (iCount > 0)
                {
                    ShowBox.MessageBox(this.Page, "删除成功!");
                    gv_Bind();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
    }
    protected void ibtnDel_Click(object sender, ImageClickEventArgs e)
    {
        CheckBox cbx = null;
        StringBuilder sbGroupNo = new StringBuilder();
        for (int i = 0; i < gvGroup.Rows.Count; i++)
        {
            cbx = (CheckBox)gvGroup.Rows[i].Cells[0].FindControl("chkSelect");
            if (cbx.Checked)
            {
                sbGroupNo.Append("'"+gvGroup.DataKeys[i].Values[1].ToString()+"',");

            }
        }

        if (sbGroupNo.Length > 0)
        {
            try
            {
                iCount = Nova_BLL_Group.DeleteAllGroup(sbGroupNo.ToString().TrimEnd(','));
                if (iCount > 0)
                {
                    ShowBox.MessageBox(this.Page, "删除成功!");
                    gv_Bind();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
    }

    /// <summary>
    /// 行绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvGroup_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            //ImageButton lbtnEdit = (ImageButton)e.Row.Cells[4].Controls[0];
            //Nova_BLL_BtnPermit.Scalarbtn(7, lbtnEdit, 2);//编辑按钮的权限

            //ImageButton ibtnDelete = (ImageButton)e.Row.Cells[5].FindControl("ibtnDelete");
            //Nova_BLL_BtnPermit.Scalarbtn(7, ibtnDelete, 1);//删除按钮的权限
        }        
    }
}
